﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LZH.Models;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq.Expressions;
using System.Data.Common;
using System.Data;
using System.Data.Entity.Infrastructure;

namespace LZH.DAL
{
    public class DAL<T>:BaseEntity where T : BaseEntity, new()
    {

        private BlogSystemContext dbcontext = new BlogSystemContext();
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="t"></param>
        public virtual int Insert(T t)
        {
            dbcontext.Set<T>().Add(t);
            return  dbcontext.SaveChanges();
        }
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="entities"></param>
        /// <returns></returns>
        public  virtual int Insert(List<T> entitys)
        {
            foreach (var entity in entitys)
            {
                dbcontext.Entry<T>(entity).State = EntityState.Added;
            }
            return dbcontext.SaveChanges();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        public virtual int Update(T t)
        {
            dbcontext.Entry(t).State = System.Data.Entity.EntityState.Modified;
             return   dbcontext.SaveChanges();
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> IQueryable()
        {
            return dbcontext.Set<T>().AsNoTracking();
        }
        /// <summary>
        /// 带条件查询
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <returns></returns>
        public virtual IQueryable<T> IQueryable(Expression<Func<T, bool>> predicate)
        {

            return IQueryable().Where(predicate);
        }
        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页面条数</param>
        /// <returns></returns>
        public  virtual IQueryable<T> IQueryable(Expression<Func<T, bool>> predicate,  int pageIndex, int pageSize = 10)
        {

            return IQueryable().Where(predicate).Skip((pageIndex-1) * pageSize).Take(pageSize);
        }

        /// <summary>
        /// 查询实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual T FindEntity(string id)
        {
            return IQueryable().First(m => m.Id == id);
        }

        /// <summary>
        /// 分页查询 + 条件查询 + 排序
        /// </summary>
        /// <typeparam name="Tkey">泛型</typeparam>
        /// <param name="pageSize">每页大小</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="total">总数量</param>
        /// <param name="whereLambda">查询条件</param>
        /// <param name="orderbyLambda">排序条件</param>
        /// <param name="isAsc">是否升序</param>
        /// <returns>IQueryable 泛型集合</returns>
        public  IQueryable<T> LoadPageItems<Tkey>(int pageSize, int pageIndex, out int total, Expression<Func<T, bool>> whereLambda, Func<T, Tkey> orderbyLambda, bool isAsc)
        {
            total = dbcontext.Set<T>().Where(whereLambda).Count();
            if (isAsc)
            {
                var temp = dbcontext.Set<T>().Where(whereLambda)
                             .OrderBy<T, Tkey>(orderbyLambda)
                             .Skip(pageSize * (pageIndex - 1))
                             .Take(pageSize);
                return temp.AsQueryable();
            }
            else
            {
                var temp = dbcontext.Set<T>().Where(whereLambda)
                           .OrderByDescending<T, Tkey>(orderbyLambda)
                           .Skip(pageSize * (pageIndex - 1))
                           .Take(pageSize);
                return temp.AsQueryable();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public virtual int Delete(T entity)
        {
            dbcontext.Entry<T>(entity).State = EntityState.Deleted;
            return dbcontext.SaveChanges();
        }
        //批量删除
        public virtual int Delete(List<T> entitys)
        {
            entitys.ForEach(m => dbcontext.Entry<T>(m).State = EntityState.Deleted);
            return dbcontext.SaveChanges();
        }
        /// <summary>
        /// 条件删除
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public virtual int Delete(Expression<Func<T, bool>> predicate)
        {
            var entitys = dbcontext.Set<T>().Where(predicate).ToList();
            entitys.ForEach(m => dbcontext.Entry<T>(m).State = EntityState.Deleted);
            return dbcontext.SaveChanges();
        }
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <returns></returnspredicate  
        public virtual List<T> FindList(string strSql)
        {
            return dbcontext.Database.SqlQuery<T>(strSql).ToList<T>();
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <param name="dbParameter">参数</param>
        /// <returns></returns>
        public virtual List<T> FindList(string strSql, DbParameter[] dbParameter)
        {
            return dbcontext.Database.SqlQuery<T>(strSql, dbParameter).ToList<T>();
        }
   
        /// <summary>
        /// 销毁
        /// </summary>
        public  void Dispose()
        {
            dbcontext.Dispose();
        }
    }
}
